const mongoose = require('mongoose');
const { Schoolmodel } = require('../model/model');

// 连接数据库 - 使用项目中的实际数据库连接
mongoose.connect('mongodb+srv://2305902315:XWpW0g8KLm20m7Ef@cluster0.v1royjo.mongodb.net/Promoted', {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log('数据库连接成功');
}).catch((err) => {
    console.log('数据库连接失败', err);
});

// 学校学号规则配置
const schoolRules = [
    {
        name: '北京大学',
        abbreviation: 'PKU',
        studentIdRule: {
            pattern: '^PKU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以PKU开头的10位学号'
        }
    },
    {
        name: '清华大学',
        abbreviation: 'THU',
        studentIdRule: {
            pattern: '^THU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以THU开头的10位学号'
        }
    },
    {
        name: '复旦大学',
        abbreviation: 'FUDAN',
        studentIdRule: {
            pattern: '^FUDAN\\d{5}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以FUDAN开头的10位学号'
        }
    },
    {
        name: '上海交通大学',
        abbreviation: 'SJTU',
        studentIdRule: {
            pattern: '^SJTU\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SJTU开头的10位学号'
        }
    },
    {
        name: '浙江大学',
        abbreviation: 'ZJU',
        studentIdRule: {
            pattern: '^ZJU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以ZJU开头的10位学号'
        }
    },
    {
        name: '南京大学',
        abbreviation: 'NJU',
        studentIdRule: {
            pattern: '^NJU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以NJU开头的10位学号'
        }
    },
    {
        name: '武汉大学',
        abbreviation: 'WHU',
        studentIdRule: {
            pattern: '^WHU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以WHU开头的10位学号'
        }
    },
    {
        name: '中山大学',
        abbreviation: 'SYSU',
        studentIdRule: {
            pattern: '^SYSU\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SYSU开头的10位学号'
        }
    },
    {
        name: '华中科技大学',
        abbreviation: 'HUST',
        studentIdRule: {
            pattern: '^HUST\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以HUST开头的10位学号'
        }
    },
    {
        name: '西安交通大学',
        abbreviation: 'XJTU',
        studentIdRule: {
            pattern: '^XJTU\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以XJTU开头的10位学号'
        }
    },
    {
        name: '北京理工大学',
        abbreviation: 'BIT',
        studentIdRule: {
            pattern: '^BIT\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以BIT开头的10位学号'
        }
    },
    {
        name: '北京师范大学',
        abbreviation: 'BNU',
        studentIdRule: {
            pattern: '^BNU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以BNU开头的10位学号'
        }
    },
    {
        name: '北京邮电大学',
        abbreviation: 'BUPT',
        studentIdRule: {
            pattern: '^BUPT\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以BUPT开头的10位学号'
        }
    },
    {
        name: '大连理工大学',
        abbreviation: 'DUT',
        studentIdRule: {
            pattern: '^DUT\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以DUT开头的10位学号'
        }
    },
    {
        name: '电子科技大学',
        abbreviation: 'UESTC',
        studentIdRule: {
            pattern: '^UESTC\\d{5}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以UESTC开头的10位学号'
        }
    },
    {
        name: '东北大学',
        abbreviation: 'NEU',
        studentIdRule: {
            pattern: '^NEU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以NEU开头的10位学号'
        }
    },
    {
        name: '东南大学',
        abbreviation: 'SEU',
        studentIdRule: {
            pattern: '^SEU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SEU开头的10位学号'
        }
    },
    {
        name: '哈尔滨工业大学',
        abbreviation: 'HIT',
        studentIdRule: {
            pattern: '^HIT\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以HIT开头的10位学号'
        }
    },
    {
        name: '华东师范大学',
        abbreviation: 'ECNU',
        studentIdRule: {
            pattern: '^ECNU\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以ECNU开头的10位学号'
        }
    },
    {
        name: '华南理工大学',
        abbreviation: 'SCUT',
        studentIdRule: {
            pattern: '^SCUT\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SCUT开头的10位学号'
        }
    },
    {
        name: '吉林大学',
        abbreviation: 'JLU',
        studentIdRule: {
            pattern: '^JLU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以JLU开头的10位学号'
        }
    },
    {
        name: '江南大学',
        abbreviation: 'JNU',
        studentIdRule: {
            pattern: '^JNU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以JNU开头的10位学号'
        }
    },
    {
        name: '兰州大学',
        abbreviation: 'LZU',
        studentIdRule: {
            pattern: '^LZU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以LZU开头的10位学号'
        }
    },
    {
        name: '南开大学',
        abbreviation: 'NKU',
        studentIdRule: {
            pattern: '^NKU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以NKU开头的10位学号'
        }
    },
    {
        name: '山东大学',
        abbreviation: 'SDU',
        studentIdRule: {
            pattern: '^SDU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SDU开头的10位学号'
        }
    },
    {
        name: '四川大学',
        abbreviation: 'SCU',
        studentIdRule: {
            pattern: '^SCU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以SCU开头的10位学号'
        }
    },
    {
        name: '天津大学',
        abbreviation: 'TJU',
        studentIdRule: {
            pattern: '^TJU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以TJU开头的10位学号'
        }
    },
    {
        name: '同济大学',
        abbreviation: 'TONGJI',
        studentIdRule: {
            pattern: '^TONGJI\\d{4}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以TONGJI开头的10位学号'
        }
    },
    {
        name: '厦门大学',
        abbreviation: 'XMU',
        studentIdRule: {
            pattern: '^XMU\\d{7}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以XMU开头的10位学号'
        }
    },
    {
        name: '中国科学技术大学',
        abbreviation: 'USTC',
        studentIdRule: {
            pattern: '^USTC\\d{6}$',
            minLength: 10,
            maxLength: 10,
            description: '请输入以USTC开头的10位学号'
        }
    }
];

// 插入学校数据
async function insertSchoolRules() {
    try {
        // 先清空现有的学校数据
        await Schoolmodel.deleteMany({});
        console.log('已清空现有学校数据');

        // 插入新的学校数据
        for (const rule of schoolRules) {
            const newSchool = new Schoolmodel(rule);
            await newSchool.save();
            console.log(`已插入 ${rule.name} 的学号规则`);
        }
        console.log('所有学校学号规则插入完成！');
    } catch (error) {
        console.error('插入失败:', error);
    } finally {
        mongoose.connection.close();
    }
}

// 执行插入
insertSchoolRules(); 